package se.teamscape.client.view;

import se.teamscape.client.presenter.AppPresenter;
import se.teamscape.client.presenter.BaseContainerPresenter.WidgetProperties;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * AppView is our top level view that represents our outermost
 * application level structure.
 * 
 * We inherit from DockLayoutPanel to get maximum flexibility in how
 * we layout the page. We use the Layout-variant from GWT 2.0 in combination
 * with standards mode to produce a more reliable cross-browser layout.
 * 
 * DockLayoutPanel allows us to define children in west, east, south, north,
 * center fashion.
 * 
 * @author Andreas Borglin
 * 
 */
public class AppView extends BaseView implements AppPresenter.Display {

    public static final int NORTH = 0;
    public static final int SOUTH = 1;
    public static final int WEST = 2;
    public static final int EAST = 3;

    private DockLayoutPanel mPanel;
    private PopupPanel mLoadingPopup;

    public AppView() {
    }

    @Override
    public void init() {
        mPanel = new DockLayoutPanel(Unit.PX);
        mLoadingPopup = new PopupPanel();
        // TODO: fix fancier load message with animated image?
        mLoadingPopup.add(new HTML("Loading data..."));
        // mLoadingPopup.setGlassEnabled(true);
        mLoadingPopup.setPopupPosition(Window.getClientWidth() / 2, Window.getClientHeight() / 2);
        // TODO: adjust for popup size as well!
    }

    @Override
    public void removeAll() {
        mPanel.clear();
    }

    @Override
    public void removeWidget(Widget w) {
        mPanel.remove(w);
    }

    @Override
    public void showWidget(Widget widget, WidgetProperties properties) {

        if (properties != null) {

            switch (properties.mLocation) {
            case NORTH:
                mPanel.addNorth(widget, properties.mSize);
                break;
            case SOUTH:
                mPanel.addSouth(widget, properties.mSize);
                break;
            case WEST:
                mPanel.addWest(widget, properties.mSize);
                break;
            case EAST:
                mPanel.addEast(widget, properties.mSize);
                break;
            default:
                mPanel.add(widget);
            }

        }
        else {
            mPanel.add(widget);
        }

    }

    @Override
    public Widget asWidget() {
        return mPanel;
    }

    @Override
    public void hideLoadingMsg() {
        mLoadingPopup.hide();
    }

    @Override
    public void showLoadingMsg() {
        mLoadingPopup.show();
    }

}
